﻿using System;
using System.Collections.Generic;
using System.Text;
using Brc_PaaS.Common.Entity;
using Brc_PaaS.Common.Helper;
using Brc_PaaS.Common.Model.Basic;
using Brc_PaaS.Common.Model.System;
using Brc_PaaS.Dal;

namespace Brc_PaaS.Bll.System
{
    public class System : ISystem
    {
        private IRepository<Bas_System> _bas_system;
        public System(IRepository<Bas_System> bas_system)
        {
            _bas_system = bas_system;
        }
       
        ResponseModel<Bas_System> ISystem.GetSystem(string systemId)
        {
            ResponseModel<Bas_System> result = new ResponseModel<Bas_System>() { IsSuccess = false, Msg = string.Empty, Data = new Bas_System() };
            if (string.IsNullOrEmpty(systemId))
            {
                result.Msg = "系统Id不能为空";
                return result;
            }
            result.Data = _bas_system.Get(i => i.Id == systemId);
            result.IsSuccess = true;
            return result;
        }

        ResponseModel<List<Bas_System>> ISystem.GetSystemList(string accountId)
        {
            ResponseModel<List<Bas_System>> result = new ResponseModel<List<Bas_System>>() { IsSuccess = false, Msg = string.Empty,Data=new List<Bas_System>() };
            if(string.IsNullOrEmpty(accountId))
            {
                result.Msg = "账户Id不能为空";
                return result;
            }
            result.Data = _bas_system.GetList(i => i.AccountId == accountId);
            result.IsSuccess = true;
            return result;           
        }

        ResponseModel<SystemCallBackModel> ISystem.RegisterSystem(Bas_System system)
        {
            ResponseModel<SystemCallBackModel> result = new ResponseModel<SystemCallBackModel>() { IsSuccess = false, Msg = string.Empty };
            if (system == null)
            {
                result.Msg = "系统信息不能为空";
                return result;
            }
            result.Msg = ChcekSystemInfo(system);
            if (!string.IsNullOrEmpty(result.Msg))
            {
                return result;
            }
            system.Id = Guid.NewGuid().ToString("N");
            system.CreateTime = DateTime.Now;
            system.Status = 1;
            if (system.AuthType == 1)
            {
                var rsaKeyDic = RSAHelper.GetPairKey(system.Id);
                system.PublicKey = rsaKeyDic.Key;
                system.PrivateKey = rsaKeyDic.Value;
            }
            result.IsSuccess = _bas_system.AddItem(system) > 0;
            result.Msg = result.IsSuccess ? "操作成功" : "操作失败,请联系管理员";
            result.Data = result.IsSuccess ? new SystemCallBackModel()
            {
                Name = system.Name,
                Ch_Name = system.Ch_Name,
                AccessKey = system.PrivateKey,
                AppKey = system.Id
            } : null;
            return result;
        }

        BaseResponseModel ISystem.UpdateSystem(Bas_System system)
        {
            ResponseModel<SystemCallBackModel> result = new ResponseModel<SystemCallBackModel>() { IsSuccess = false, Msg = string.Empty };
            if (system == null)
            {
                result.Msg = "系统信息不能为空";
                return result;
            }
            result.Msg = ChcekSystemInfo(system);
            if (!string.IsNullOrEmpty(result.Msg))
            {
                return result;
            }
            var dbSystem = _bas_system.Get(i => i.Id == system.Id);
            if(dbSystem==null)
            {
                result.Msg = "未能获取到对应系统信息";
                return result;
            }
            if(system.AuthType==0)
            {
                system.PrivateKey = string.Empty;
                system.PublicKey = string.Empty;
            }
            else
            {
                var rsaKeyDic = RSAHelper.GetPairKey(system.Id);
                system.PublicKey = rsaKeyDic.Key;
                system.PrivateKey = rsaKeyDic.Value;
            }
            result.IsSuccess = _bas_system.Update(system) > 0;
            result.Msg = result.IsSuccess ? "操作成功" : "操作失败,请联系管理员";
            return result;

        }
        private string ChcekSystemInfo(Bas_System system)
        {
            if (string.IsNullOrEmpty(system.Name))
            {
                return "系统名称不能为空";
            }
            if (string.IsNullOrEmpty(system.AccountId))
            {
                return "未能获取到系统注册账号信息";
            }
            return string.Empty;
        }
    }
}
